<template>
  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="车辆品牌" align="center" prop="vehicleBrand" />
      <el-table-column label="车辆型号" align="center" prop="vehicleModel" />
      <el-table-column
        label="购置日期"
        align="center"
        prop="purchaseDate"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="行驶里程（公里）" align="center" prop="mileage" />
      <el-table-column label="评估价值（元）" align="center" prop="assessedValue" />
      <el-table-column label="登记证书编号" align="center" prop="registrationNumber" />
      <el-table-column label="行驶证照片" align="center" prop="licensePhoto" >
          <template #default="scope">
            <el-image
                style="width: 100px; height: 100px"
                :src="scope.row.licensePhoto"
                :zoom-rate="1.2"
                :preview-src-list="[scope.row.licensePhoto]"
                :initial-index="0"
                fit="cover"
                hide-on-click-modal
                preview-teleported
            />
          </template>
      </el-table-column>
      <el-table-column label="抵押状态" align="center" prop="mortgageStatus">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.MORTGAGE" :value="scope.row.mortgageStatus" />
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

</template>

<script setup lang="ts">
import { getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { CarMortgageApi, CarMortgageVO } from '@/api/system/carmortgage'
import CarMortgageForm from './CarMortgageForm.vue'

/** 车辆抵押贷款信息 列表 */
defineOptions({ name: 'CarMortgage' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const props = defineProps<{
  userId: number
}>()
const loading = ref(true) // 列表的加载中
const list = ref<CarMortgageVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  userId: props.userId,
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
const imageRef = ref<ImageInstance>()
const showPreview = ref(false)

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await CarMortgageApi.getCarMortgagePage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}
const handleClick = () => {
  imageRef.clickHandler() // 手动触发预览
}
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await CarMortgageApi.deleteCarMortgage(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await CarMortgageApi.exportCarMortgage(queryParams)
    download.excel(data, '车辆抵押贷款信息.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
